//
//$Id: utils.h 649 2011-02-18 18:30:44Z andrew.tsyganov $
#pragma once

#include "sets.h"
#include "types.h"
#include "contrib/rapidxml/rapidxml.hpp"
#include <algorithm>

inline std::string IntToString(u_t val)
{
    char buff[32];
    sprintf(buff, "%u", val);

    return std::string(buff);
}

inline u_t Width(u_t const val)
{
    u_t w = 0;
    for (u_t n = val; n > 0; n /= 10)
    {
        ++w;
    }

    return w;
}

inline std::string IntToStringZ(u_t const val, u_t const max)
{
    char buff[32];
    sprintf(buff, "%0*u", Width(max), val);

    return std::string(buff);
}

inline std::string ToLower(std::string const& s)
{
    std::string str = s;
    transform(str.begin(), str.end(), str.begin(), tolower);
    return str;
}

inline std::string ToLower(char const* s)
{
    std::string str(s);
    transform(str.begin(), str.end(), str.begin(), tolower);
    return str;
}

inline double log2(double x)
{
    return log(x)/log(2.0);
}

inline u_t power2(u_t n)
{
    u_t power = 1;

    for (u_t i = 0; i < n; ++i)
    {
        power *= 2;
    }

    return power;
}

inline void PrintLine(std::ostream& os, u_t const width, char const sym = '-')
{
    for (u_t i = 0; i < width; ++i)
    {
        os << sym; 
    }
    os << std::endl;
}

std::string OpenBracket(Brackets_Type const bt);
std::string CloseBracket(Brackets_Type const bt);
std::string MakeLabel(std::string const& pfx, u_t const id, Labels_Type const lt);
std::string StateInOutLabel(bool const in, bool const out,  Labels_Type const lt);

std::string GetFileExtension(std::string const& filename);
std::string CreatePath(std::string const& dir, std::string const& file);
void SplitFileName(std::string const& path, std::string& dir, std::string& file);
void SplitFileName(std::string const& path, std::string& dir, std::string& name, std::string& ext);

u_t Min(u_t a, u_t b, u_t c);

bool EquivalentRows(Bool_Matrix const& m, u_t const i, u_t const j); 
bool EquivalentColumns(Bool_Matrix const& m, u_t const i, u_t const j); 
bool AllOnesInRow(Bool_Matrix const& m, u_t const i, Subset const& s);
bool AllOnesInColumn(Bool_Matrix const& m, u_t const j, Subset const& s);
u_t OnesCount(Bool_Matrix const& m);

rapidxml::xml_node<>* GetNode(std::string const& file, std::vector<char>& buffer, rapidxml::xml_document<>& doc, std::string const& NodeName);
bool HasFANode(std::string const& file);
bool HasRENode(std::string const& file);
//EOF!
